package com.danceforyou.medium.service.impl;

import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.service.OperationService;
import com.danceforyou.common.service.impl.BaseServiceImpl;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.medium.dao.MediumLabelDAO;
import com.danceforyou.medium.model.MediumLabelDO;
import com.danceforyou.medium.service.MediumLabelService;
import com.danceforyou.medium.vo.MediumLabelVO;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
* 类名称：MediumLabelServiceImpl
* 创建人：danceforyou
* 创建时间：2021年01月26日
* 版本号：1.0v
*/
@Service("mediumLabelService")
public class MediumLabelServiceImpl extends BaseServiceImpl<Long, MediumLabelDO, MediumLabelVO, MediumLabelDAO> implements MediumLabelService {
    @Autowired
    private MediumLabelDAO mediumLabelDAO;
    @Autowired
    private OperationService operationService;
    @Autowired(required = false)
    private HttpSession session;

    @Override
    public MediumLabelDAO getDao() {
        return mediumLabelDAO;
    }

    @Override
    public void saveInfo(MediumLabelDO mediumLabel) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        MediumLabelDO temp = this.getByCondition(new HashMap<String, Object>(){{
            put("labelName", mediumLabel.getLabelName());
            put("type", mediumLabel.getType());
        }});
        AssertUtil.isNull(temp, "该类别已存在，操作失败");
        mediumLabelDAO.insert(mediumLabel);
        operationService.saveOperation(Constants.OPER_TABLE_MEDIUM_LABEL, mediumLabel.getId(), Constants.OPER_ADD, loginUser, null);
    }

    @Override
    public int deleteInfoByIds(String ids) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        String[] idArr = ids.split(",");
        int i = 0;
        for (String id : idArr) {
            MediumLabelDO mediumLabel = mediumLabelDAO.getById(Long.valueOf(id));
            AssertUtil.notNull(mediumLabel, "信息不存在");
            i += this.deleteById(Long.valueOf(id));
            operationService.saveOperation(Constants.OPER_TABLE_MEDIUM_LABEL, mediumLabel.getId(), Constants.OPER_DELETE, loginUser, null);
        }
        return i;
    }
}
